<?php
/**
 * Created by JetBrains PhpStorm.
 * User: MASTER
 * Date: 10/07/13
 * Time: 16:58
 * To change this template use File | Settings | File Templates.
 */

class globalData extends Controller
{    
    static function read()
    {
        $funArgs = func_get_args();
        $modelName = $funArgs[0];
        if(func_num_args() > 1){    
            array_shift($funArgs);            
            $model = $modelName::model()->with($funArgs)->findAll();            
            foreach ($model as $allRows) {              
                foreach ($allRows as $key => $value) {                    
                    $getData[$key] = $value;
                }
                foreach ($funArgs as $nameRelation) {
                    $contKey = 0;
                    foreach ($allRows->$nameRelation as $key => $value) {                        
                        $getData[$key] = $value;
                    }
                }
                $getDataRows[] = $getData;
            }           
        } else {
            $getDataRows = array();
            $getDataField= array();
            $model = $modelName::model()->findAll();            
            foreach ($model as $nameObj) {
                foreach ($nameObj as $key => $value) {                    
                    $getDataField[$key] = $value;
                }
                $getDataRows[] = $getDataField;
            }           
        }
        return $getDataRows;
        //echo CJSON::encode(array('data' => $getDataRows, 'success' => true));
    }
    static function create($nameModel, $nameClass)
    {
        $swKey = false;
        $model = new $nameModel;
        $transaction = Yii::app()->db->beginTransaction();
        try {
            $model->id = count($nameModel::model()->findAll()) + 1;
            $model->attributes = json_decode($_REQUEST['data'], true);
            $swKey = $model->save();
            $transaction->commit();
            return $model;
        } catch(Exception $e) {
            $swKey = false;
            $transaction->rollback();
            echo CJSON::encode(array('errorClass' => $nameClass));
        }
    }
    static function update($nameModel, $nameClass)
    {
        $swKey = false;        
        $transaction = Yii::app()->db->beginTransaction();
        try{
            $data = json_decode($_REQUEST['data'], true);
            $model = $nameModel::model()->find("id=:keyId", array(':keyId' => $data['id']));
            $model->attributes = $data;
            $widKey = $model->save();
            $transaction->commit();
            return $model;
        } catch(Exception $e) {
            $swKey = false;
            $transaction->rollback();
            echo CJSON::encode(array('errorClass' => $nameClass));
        }
    }
    static function delete($nameModel, $nameClass)
    {
        $swKey = false;
        $ids = json_decode($_REQUEST['data'], true);
        $transaction = Yii::app()->db->beginTransaction();
        try {            
            $prefix = substr($nameModel, 4, 3).'_estado';
            foreach ($ids as $value) {
                $model = $nameModel::model()->find("id=:keyId", array(':keyId' => is_null($value['id'])? $value: $value['id']));
                $model->$prefix = 0;
                $swKey = $model->save();
            }
            $transaction->commit();
            return $model;
        } catch(Exception $e) {
            $swKey = false;
            $transaction->rollback();
            echo CJSON::encode(array('errorClass' => $nameClass));
        }        
    }
}